﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;
using AiXiu.IDAL;
using System.Data.Entity;

namespace AiXiu.DAL
{
    /// <summary>
    /// 用户操作类
    /// </summary>
    public class UserService : IUserService
    {
        AiXiuDbContext db = new AiXiuDbContext();
        /// <summary>
        /// 根据Id查询用户信息
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns>用户名为 Id 的用户信息</returns>
        public TBUsers getUserById(int Id)
        {
            return db.TBUsers.FirstOrDefault(t => t.Id == Id);  
        }
 
        public int editUserInfo(TBUsers newUser)
        {
            TBUsers oldUser = db.TBUsers.FirstOrDefault(t => t.Id == newUser.Id);

            oldUser.NickName = newUser.NickName;
            oldUser.Sex = newUser.Sex;
            oldUser.Hobby = newUser.Hobby;
            oldUser.Birthday = newUser.Birthday;
            oldUser.Address = newUser.Address;

            //保存到数据库
            db.Entry(oldUser).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public int editAvatar(TBUsers newUser)
        {
            TBUsers oldUser = db.TBUsers.FirstOrDefault(t => t.Id == newUser.Id);
            
            oldUser.Avatar = newUser.Avatar;

            db.Entry(oldUser).State = EntityState.Modified;
            return db.SaveChanges();
        }

    }
}
